<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Myvideo extends Controller_Common {

	public $template = 'layout/index';

	public function action_index() {
		$current_category = DB::select()->from('videocat')->order_by('sort', 'DESC')->limit(1)->as_object()->execute()->current();
		$this->redirect('myvideo/'.$current_category->id);
	}

	public function action_showCategory() {
		$category_id = $this->request->param('id');

		if (empty($category_id)) {
			$current_category = DB::select()->from('videocat')->order_by('id', 'DESC')->limit(1)->as_object()->execute()->current();
		} else {
			$current_category = Model::factory('Videocat')->getOne($this->request->param('id'));
		}

		$per_page = $this->_config->get('photos_per_page');
		$submenu = Model::factory('Videocat')->getAll();
		$videos = Model::factory('Video')->getCategoryList($current_category->id, $this->getOffset($per_page));
		$videos_total = ORM::factory('Video')->where('category_id', '=', $current_category->id)->count_all();
		$this->buildCarcas(array($submenu, 'myvideo'));
		$this->template->title .= ' - Мои видео - '.$current_category->title;

		$this->template->content = View::factory('pieces/videos', array(
			'videos' => $videos,
            'pagination' => Pagination::factory(array(
            	'view' => 'pagination/basic_ru',
                'total_items' => $videos_total,
                'items_per_page' => $per_page,
            ))
		));
	}

}